# 五子棋游戏 HTML 实现

以下是一个完整的五子棋游戏 HTML 实现，所有代码都在一个 HTML 文件中：

```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>五子棋游戏</title>
    <style>
        body {
            font-family: 'Microsoft YaHei', sans-serif;
            display: flex;
            flex-direction: column;
            align-items: center;
            background-color: #f5f5dc;
            margin: 0;
            padding: 20px;
        }
        
        h1 {
            color: #8B4513;
            margin-bottom: 10px;
        }
        
        .game-container {
            display: flex;
            flex-direction: column;
            align-items: center;
            margin-top: 20px;
        }
        
        .board {
            display: grid;
            grid-template-columns: repeat(15, 30px);
            grid-template-rows: repeat(15, 30px);
            background-color: #DEB887;
            border: 2px solid #8B4513;
            position: relative;
        }
        
        .cell {
            width: 30px;
            height: 30px;
            box-sizing: border-box;
            position: relative;
            cursor: pointer;
        }
        
        .cell::before, .cell::after {
            content: '';
            position: absolute;
            background-color: #000;
        }
        
        .cell::before {
            width: 100%;
            height: 1px;
            top: 50%;
            left: 0;
            transform: translateY(-50%);
        }
        
        .cell::after {
            width: 1px;
            height: 100%;
            left: 50%;
            top: 0;
            transform: translateX(-50%);
        }
        
        .piece {
            position: absolute;
            width: 26px;
            height: 26px;
            border-radius: 50%;
            top: 2px;
            left: 2px;
            z-index: 1;
        }
        
        .black {
            background: radial-gradient(circle at 30% 30%, #666, #000);
            box-shadow: 1px 1px 2px rgba(0, 0, 0, 0.7);
        }
        
        .white {
            background: radial-gradient(circle at 30% 30%, #fff, #ccc);
            box-shadow: 1px 1px 2px rgba(0, 0, 0, 0.7);
        }
        
        .controls {
            margin-top: 20px;
            display: flex;
            gap: 15px;
        }
        
        button {
            padding: 8px 15px;
            background-color: #8B4513;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s;
        }
        
        button:hover {
            background-color: #A0522D;
        }
        
        .status {
            margin-top: 15px;
            font-size: 18px;
            font-weight: bold;
            color: #8B4513;
            height: 24px;
        }
        
        .star-point {
            position: absolute;
            width: 6px;
            height: 6px;
            background-color: #000;
            border-radius: 50%;
            transform: translate(-50%, -50%);
            z-index: 1;
        }
    </style>
</head>
<body>
    <h1>五子棋游戏</h1>
    <div class="game-container">
        <div class="status" id="status">黑方回合</div>
        <div class="board" id="board"></div>
        <div class="controls">
            <button id="restart">重新开始</button>
            <button id="undo">悔棋</button>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const board = document.getElementById('board');
            const statusDisplay = document.getElementById('status');
            const restartButton = document.getElementById('restart');
            const undoButton = document.getElementById('undo');
            
            const BOARD_SIZE = 15;
            let currentPlayer = 'black';
            let gameActive = true;
            let gameState = Array(BOARD_SIZE).fill().map(() => Array(BOARD_SIZE).fill(null));
            let moveHistory = [];
            
            // 初始化棋盘
            function initializeBoard() {
                board.innerHTML = '';
                
                // 创建棋盘格子
                for (let i = 0; i < BOARD_SIZE; i++) {
                    for (let j = 0; j < BOARD_SIZE; j++) {
                        const cell = document.createElement('div');
                        cell.classList.add('cell');
                        cell.dataset.row = i;
                        cell.dataset.col = j;
                        cell.addEventListener('click', handleCellClick);
                        board.appendChild(cell);
                    }
                }
                
                // 添加星位点
                const starPoints = [
                    [3, 3], [3, 11], [3, 7],
                    [11, 3], [11, 11], [11, 7],
                    [7, 3], [7, 11], [7, 7]
                ];
                
                starPoints.forEach(point => {
                    const star = document.createElement('div');
                    star.classList.add('star-point');
                    star.style.left = `${(point[1] * 30) + 15}px`;
                    star.style.top = `${(point[0] * 30) + 15}px`;
                    board.appendChild(star);
                });
            }
            
            // 处理格子点击
            function handleCellClick(e) {
                if (!gameActive) return;
                
                const row = parseInt(e.target.dataset.row);
                const col = parseInt(e.target.dataset.col);
                
                // 如果该位置已有棋子，则不能下
                if (gameState[row][col] !== null) return;
                
                // 记录这一步
                moveHistory.push({row, col, player: currentPlayer});
                
                // 更新游戏状态
                gameState[row][col] = currentPlayer;
                
                // 在棋盘上放置棋子
                const piece = document.createElement('div');
                piece.classList.add('piece', currentPlayer);
                e.target.appendChild(piece);
                
                // 检查是否获胜
                if (checkWin(row, col)) {
                    statusDisplay.textContent = `${currentPlayer === 'black' ? '黑方' : '白方'}获胜！`;
                    gameActive = false;
                    return;
                }
                
                // 切换玩家
                currentPlayer = currentPlayer === 'black' ? 'white' : 'black';
                statusDisplay.textContent = `${currentPlayer === 'black' ? '黑方' : '白方'}回合`;
            }
            
            // 检查是否获胜
            function checkWin(row, col) {
                const directions = [
                    [0, 1],   // 水平
                    [1, 0],    // 垂直
                    [1, 1],    // 对角线
                    [1, -1]    // 反对角线
                ];
                
                for (const [dx, dy] of directions) {
                    let count = 1;
                    
                    // 正向检查
                    for (let i = 1; i <= 4; i++) {
                        const newRow = row + i * dx;
                        const newCol = col + i * dy;
                        
                        if (newRow < 0 || newRow >= BOARD_SIZE || newCol < 0 || newCol >= BOARD_SIZE) break;
                        if (gameState[newRow][newCol] !== currentPlayer) break;
                        
                        count++;
                    }
                    
                    // 反向检查
                    for (let i = 1; i <= 4; i++) {
                        const newRow = row - i * dx;
                        const newCol = col - i * dy;
                        
                        if (newRow < 0 || newRow >= BOARD_SIZE || newCol < 0 || newCol >= BOARD_SIZE) break;
                        if (gameState[newRow][newCol] !== currentPlayer) break;
                        
                        count++;
                    }
                    
                    if (count >= 5) return true;
                }
                
                return false;
            }
            
            // 重新开始游戏
            function restartGame() {
                gameState = Array(BOARD_SIZE).fill().map(() => Array(BOARD_SIZE).fill(null));
                currentPlayer = 'black';
                gameActive = true;
                moveHistory = [];
                statusDisplay.textContent = '黑方回合';
                initializeBoard();
            }
            
            // 悔棋
            function undoMove() {
                if (moveHistory.length === 0 || !gameActive) return;
                
                const lastMove = moveHistory.pop();
                gameState[lastMove.row][lastMove.col] = null;
                
                const cell = document.querySelector(`[data-row="${lastMove.row}"][data-col="${lastMove.col}"]`);
                cell.innerHTML = '';
                
                currentPlayer = lastMove.player;
                statusDisplay.textContent = `${currentPlayer === 'black' ? '黑方' : '白方'}回合`;
            }
            
            // 事件监听
            restartButton.addEventListener('click', restartGame);
            undoButton.addEventListener('click', undoMove);
            
            // 初始化游戏
            initializeBoard();
        });
    </script>
</body>
</html>
```

## 游戏功能说明

1. **基本规则**：
   - 15×15 标准五子棋棋盘
   - 黑方先行，轮流下子
   - 先形成五子连线者获胜

2. **游戏特性**：
   - 显示当前回合玩家
   - 获胜时显示获胜方
   - 重新开始按钮
   - 悔棋功能

3. **界面设计**：
   - 木质风格的棋盘
   - 黑白棋子有立体效果
   - 标出了传统五子棋的星位点

4. **技术实现**：
   - 纯 HTML/CSS/JavaScript 实现
   - 使用 CSS Grid 布局棋盘
   - 使用二维数组跟踪游戏状态
   - 实现四方向获胜检查算法

您可以将此代码保存为单个 HTML 文件，然后在浏览器中打开即可开始游戏。